<?php

/**
 * 分销中心（佣金明细） - AM
 * 版本：2.0
 */

if (!defined('IN_IA')) {
	die('Access Denied');
}
global $_W, $_GPC;

$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';

$openid = m('user')->getOpenid();
$member = m('member')->getMember($openid);

if($operation == 'display'){

	if($_W['isajax']){
		$pindex = max(1, intval($_GPC['page']));
		$psize = 20;
		$status = trim($_GPC['status']);

		$statusParam = array();
		$key = '';

		switch($status){
			case -1 :
				$key = 'invalid';
				break;
			case 1 :
				$key = 'apply';
				break;
			case 2 :
				$key = 'check';
				break;
			case 3 :
				$key = 'pay';
				break;
			default :
				$key = 'all';
				break;
		}

		$commissionApplyLists = $this->model->getCommissionApply($member['id'], array($key), $pindex, $psize);
		$commissionCount = $this->model->getCommissionApplySum($member['id'], array($key));

		foreach($commissionApplyLists[$key] as &$row){
			if ($row['status'] == 1) {
				$row['statusstr'] = '待审核';
				$row['dealtime'] = date('Y-m-d H:i', $row['applytime']);
			} else {
				if ($row['status'] == 2) {
					$row['statusstr'] = '待打款';
					$row['dealtime'] = date('Y-m-d H:i', $row['checktime']);
				} else {
					if ($row['status'] == 3) {
						$row['statusstr'] = '已打款';
						$row['dealtime'] = date('Y-m-d H:i', $row['checktime']);
					} else {
						if ($row['status'] == -1) {
							$row['dealtime'] = date('Y-m-d H:i', $row['invalidtime']);
							$row['statusstr'] = '无效';
						}
					}
				}
			}

			$row['commission_pay'] = $this->model->getCommissionApplyPayPrice($member['id'], $row['id']);
		}
		unset($row);

		show_json(1, array('total' => $commissionApplyLists[$key.'count'], 'list' => $commissionApplyLists[$key], 'pagesize' => $psize, 'commissioncount' => number_format($commissionCount[$key], 2)));
	}

	include $this->template('log');
}

if($operation == 'detail' || $operation == 'detail_order'){

	$id = intval($_GPC['id']);
	$applyDetail = $this->model->getCommissionApplyDetail($id);
	$orderids = iunserializer($applyDetail['orderids']);

	if($_W['isajax']){
		if(empty($applyDetail)){
			show_json(0, array('message' => '未找到提现申请!'));
		}

		if (!is_array($orderids) || count($orderids) <= 0) {
			show_json(0, array('message' => '未找到订单信息!'));
		}
	}

	if($_W['isajax'] && $operation == 'detail'){
		show_json(1, array('apply' => $applyDetail));
	}

	if($_W['isajax'] && $operation == 'detail_order'){
		$pindex = max(1, intval($_GPC['page']));
		$psize = 5;

		$orderids = array_slice($orderids, ($pindex - 1) * $psize, $psize);

		$result = $this->model->getCommissionApplyOrderDetail($member['id'], $orderids);

		if(empty($result['lists'])){
			$result['lists'] = array();
		}

		show_json(1, array('list' => $result['lists'], 'pagesize' => $psize, 'totalcommission' => $result['totalcommission']));
	}

	include $this->template('log_detail');
}